package chainofresponsibility;

import java.util.ArrayList;

/**
 * 消息
 */
class Msg{
    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    String str;

    @Override
    public String toString() {
        return "Msg{" +
                "str='" + str + '\'' +
                '}';
    }
}

interface Filter{
    boolean doFilter(Msg msg);
}

class FilterChain implements Filter{
    private ArrayList<Filter> list = new ArrayList<>();

    public FilterChain add(Filter filter){
        list.add(filter);
        return this;
    }

    @Override
    public boolean doFilter(Msg msg) {
        for(Filter filter : list){
            if(!filter.doFilter(msg)){
                return false;
            }
        }
        return true;
    }
}


public class ChainOfResponsibility {
    public static void main(String[] args) {
        FilterChain filterManage = new FilterChain();
        Msg msgs = new Msg();
        msgs.str = "admin";

        filterManage.add((Msg msg)->{
            msg.str = msg.str.replaceAll("admin", "tig");
            return true;
        }).add((Msg msg)->{
            msg.str = msg.str.replaceAll("tig", "lig");
            return true;
        }).doFilter(msgs);

        System.out.println(msgs);
    }
}
